﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Silver3D.Math;

namespace Silver3D.Tests.Math
{
    /// <summary>
    /// Summary description for Matrix33Tests
    /// </summary>
    [TestClass]
    public class Matrix33Tests
    {
        public Matrix33Tests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void Matrix33_Constructors()
        {
            Matrix33 m = new Matrix33();
            Assert.AreEqual(0.0, m.M11);
            Assert.AreEqual(0.0, m.M23);
            Assert.AreEqual(0.0, m.M33);

            Matrix33 n = new Matrix33(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
            Assert.AreEqual(8.0, n.M32);

            Matrix33 o = new Matrix33(n);
            Assert.AreEqual(7.0, n.M31);
        }

        [TestMethod]
        public void Matrix33_Inverse()
        {
            Matrix33 m = new Matrix33(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
            Assert.IsFalse(m.IsInvertible);
            Assert.AreEqual(0.0, m.Determinant);

            Matrix33 n = new Matrix33(1, 2, 3, 3, 5, 8, 0, 0, 1);
            Assert.AreEqual(-1.0, n.Determinant);
            Assert.IsTrue(n.IsInvertible);
            Matrix33 ni = n.Inverse;

            Assert.IsTrue(ni == new Matrix33(-5, 2, -1, 3, -1, -1, 0, 0, 1));
        }

        [TestMethod]
        public void Matrix33_Multiplication()
        {
            Matrix33 a = new Matrix33(1, 2, 3, 4, 5, 6, 7, 8, 9);
            Matrix33 b = new Matrix33(9, 8, 7, 6, 5, 4, 3, 2, 1);

            Assert.AreEqual(new Matrix33(30, 24, 18, 84, 69, 54, 138, 114, 90), a * b);
        }

        [TestMethod]
        public void Matrix33_Transforms()
        {
            Vec2 v = Vec2.XAxis;
            Matrix33 r = Matrix33.CreateRotationZ(-System.Math.PI / 2.0);

            Assert.IsTrue(Vec2.YAxis.IsEquivalent(r.TransformVector(v)));
        }
    }
}
